package com.ruoyi.project.system.record.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.record.domain.TDiseaseRecord;
import com.ruoyi.project.system.record.domain.TTreatmentRecord;
import com.ruoyi.project.system.record.service.ITDiseaseRecordService;
import com.ruoyi.project.system.record.service.ITTreatmentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新生儿疾病治疗和随访记录Controller
 *
 * @author
 * @date 2021-03-31
 */
@Controller
@RequestMapping("/system/treatmentAndFollowUp")
public class TTreatmentAndFollowUpRecord extends BaseController
{
    private String prefix = "system/treatmentAndFollowUp";
    private String treatmentPrefix = "system/treatmentRecord";
    private String followUpPrefix = "system/followUprecord";

    @Autowired
    private ITDiseaseRecordService tDiseaseRecordService;

    @Autowired
    private ITTreatmentRecordService treatmentRecordService;

    @GetMapping()
    public String record()
    {
        return prefix + "/list";
    }

    /**
     * 查询新生儿待治疗和治疗中疾病记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TDiseaseRecord tDiseaseRecord)
    {
        startPage();
        List<TDiseaseRecord> list = tDiseaseRecordService.selectTDiseaseInTreatRecordList(tDiseaseRecord);
        return getDataTable(list);
    }

    /**
     * 导出新生儿疾病记录列表
     */
    @Log(title = "新生儿疾病记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TDiseaseRecord tDiseaseRecord)
    {
        List<TDiseaseRecord> list = tDiseaseRecordService.selectTDiseaseRecordList(tDiseaseRecord);
        ExcelUtil<TDiseaseRecord> util = new ExcelUtil<TDiseaseRecord>(TDiseaseRecord.class);
        return util.exportExcel(list, "diseaseRecord");
    }

    /**
     * 新增新生儿疾病记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新生儿疾病记录
     */
    @Log(title = "新生儿疾病记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TDiseaseRecord tDiseaseRecord)
    {
        return toAjax(tDiseaseRecordService.insertTDiseaseRecord(tDiseaseRecord));
    }

    /**
     * 修改新生儿疾病记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TDiseaseRecord tDiseaseRecord = tDiseaseRecordService.selectTDiseaseRecordById(id);
        mmap.put("tDiseaseRecord", tDiseaseRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存新生儿疾病记录
     */
    @Log(title = "新生儿疾病记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TDiseaseRecord tDiseaseRecord)
    {
        return toAjax(tDiseaseRecordService.updateTDiseaseRecord(tDiseaseRecord));
    }

    /**
     * 删除新生儿疾病记录
     */
    @Log(title = "新生儿疾病记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tDiseaseRecordService.deleteTDiseaseRecordByIds(ids));
    }

    /**
     * 新生儿疾病治疗
     */
    @GetMapping("/treatment/{id}")
    public String treatment(@PathVariable("id") Long id, ModelMap mmap)
    {
        TTreatmentRecord tTreatmentRecord = new TTreatmentRecord();
        int count = treatmentRecordService.selectCountByDiseaseId(id);
        tTreatmentRecord.setCurrentTreatTimes((long)count + 1);
        tTreatmentRecord.setDiseaseId(id);
        mmap.put("tTreatmentRecord", tTreatmentRecord);
        return treatmentPrefix + "/edit";
    }

    /**
     * 新生儿疾病治疗
     */
    @Log(title = "新生儿疾病治疗记录保存", businessType = BusinessType.UPDATE)
    @PostMapping("/treatmentSave")
    @ResponseBody
    public AjaxResult treatmentSave(TDiseaseRecord tDiseaseRecord)
    {
        return toAjax(tDiseaseRecordService.updateTDiseaseRecord(tDiseaseRecord));
    }


    /**
     * 新生儿疾病结束治疗
     */
    @PostMapping("/stop")
    @ResponseBody
    public AjaxResult stop(TDiseaseRecord tDiseaseRecord)
    {
        return toAjax(tDiseaseRecordService.stop(tDiseaseRecord));
    }
}
